package com.nju.crawler.remote;

import cn.hutool.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

/**
 * @authorliyunfei
 * @date2022/11/21
 **/
@Component
public class KafkaGithubInfoProducer {
       @Autowired
       KafkaTemplate<String,Object> kafkaTemplate;

       //@PostConstruct
       public void testAck(){
              // 测试Kafka生产
              //JSONArray batchTop = new JSONArray();
              int i = 0;
              while(i<10){
                  try {
                      //Caused by: org.apache.kafka.common.errors.TimeoutException: Topic test-bigdata-1 not present in metadata after 60000 m
                      // 捕获不了异常》》？PostConstruct初始化信息---
                      kafkaTemplate.send("test-bigdata-1","hello,i am server test packet-"+i);
                      TimeUnit.SECONDS.sleep(10);
                  } catch (InterruptedException e) {
                      //TODO 日志框架
                      System.out.println("send failed");
                      //throw new RuntimeException(e);
                  } finally {
                      i++;
                  }
              }
       }
}
